package com.tuyan.aixiang.component;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;


import com.tuyan.aixiang.R;
import com.tuyan.aixiang.enums.LaunchMode;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

public class BYTab extends LinearLayout {

    private static final String TAG = "BYTab";

    private List<BYTabItem> tabs = new ArrayList<>();

    private OnTabChangeListener changeListener;

    private Context context;

    private ViewPager viewPager;

    public BYTab(Context context) {
        this(context, null);
    }

    public BYTab(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BYTab(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
//        this.init();

    }

    public void init(Integer mode) {
        tabs = new ArrayList<>();
        removeAllViews();

        BYTabItem.OnTabClickListener tabClickListener = idx -> handleTabChange(idx);

        BYTabItem itemControl = new BYTabItem(context);
        itemControl.setup( R.string.tab_control, R.drawable.control, R.drawable.control_enable);
        itemControl.setOnTabClickListener(tabClickListener);

        BYTabItem itemMap = new BYTabItem(context);
        itemMap.setup(R.string.tab_map, R.drawable.map, R.drawable.map_enable);
        itemMap.setOnTabClickListener(tabClickListener);

        BYTabItem itemMsg = new BYTabItem(context);
        itemMsg.setup(R.string.tab_msg, R.drawable.msg, R.drawable.msg_enable);
        itemMsg.setOnTabClickListener(tabClickListener);

        BYTabItem itemMe = new BYTabItem(context);
        itemMe.setup(R.string.tab_me, R.drawable.me, R.drawable.me_enable);
        itemMe.setOnTabClickListener(tabClickListener);

        tabs.add(itemControl);
        tabs.add(itemMsg);
        if (mode.equals(LaunchMode.ENTERPRISE.getValue())) {
            tabs.add(itemMap);
        }
        tabs.add(itemMe);


        setBackgroundColor(Color.WHITE);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        for (int i = 0; i < tabs.size(); i++) {
            BYTabItem item = tabs.get(i);
            item.setIndex(i);
            addView(item, i);
        }
        handleTabChange(0);

    }

    public void setupWithPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                handleTabChange(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        handleTabChange(0);
    }

    private void handleTabChange(int idx) {
        Log.d(TAG, "handleTabChange" + idx);
        for (int i = 0; i < tabs.size(); i++) {
            BYTabItem item = tabs.get(i);
            if (idx == i) {
                item.enable();
            } else {
                item.disable();
            }
        }
        if (null != this.changeListener) {
            this.changeListener.apply(idx);
        }
        if (null != this.viewPager) {
            this.viewPager.setCurrentItem(idx);
        }
    }

    public void setOnTabChangeListener(OnTabChangeListener listener) {
        this.changeListener = listener;

    }


    public interface OnTabChangeListener {
        void apply(int idx);
    }


}
